﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Threading;
using System.Windows.Threading;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace ImaginationFactory.Dialogs
{
    /// <summary>
    /// MainListBox.xaml 的交互逻辑
    /// </summary>
    public partial class MainListBox : Window
    {
        MainWindow mWindowTemp;

        public MainListBox( MainWindow mWindow )
        {
            InitializeComponent();
            mWindowTemp = mWindow;


            //菜单弹出动画

            DoubleAnimation ListBoxBorderHeight1 = new DoubleAnimation();
            DoubleAnimation ListBoxBorderHeight2 = new DoubleAnimation();
            DoubleAnimation ListBoxBorderOpacity1 = new DoubleAnimation();
            DoubleAnimation ListBoxBorderOpacity2 = new DoubleAnimation();
            DoubleAnimation ListBoxOpacity1 = new DoubleAnimation();
            DoubleAnimation ListBoxOpacity2 = new DoubleAnimation();

            ListBoxBorderHeight1.To = 550;
            ListBoxBorderHeight1.AccelerationRatio = 0.5;
            ListBoxBorderHeight1.Duration = new Duration( TimeSpan.FromSeconds( 0.1 ) );
            ListBoxBorderHeight1.BeginTime = TimeSpan.FromSeconds( 0 );

            ListBoxBorderHeight2.To = 450;
            ListBoxBorderHeight2.AccelerationRatio = 0.3;
            ListBoxBorderHeight2.Duration = new Duration( TimeSpan.FromSeconds( 0.2 ) );
            ListBoxBorderHeight2.BeginTime = TimeSpan.FromSeconds( 0.25 );

            ListBoxBorderOpacity1.To = 1;
            ListBoxBorderOpacity1.Duration = new Duration( TimeSpan.FromSeconds( 0.1 ) );
            ListBoxBorderOpacity1.BeginTime = TimeSpan.FromSeconds( 0.45 );

            ListBoxBorderOpacity2.To = 0.95;
            ListBoxBorderOpacity2.Duration = new Duration( TimeSpan.FromSeconds( 0.2 ) );
            ListBoxBorderOpacity2.BeginTime = TimeSpan.FromSeconds( 0.65 );

            ListBoxOpacity1.To = 1;
            ListBoxOpacity1.Duration = new Duration( TimeSpan.FromSeconds( 0.1 ) );
            ListBoxOpacity1.BeginTime = TimeSpan.FromSeconds( 0.55 );

            ListBoxOpacity2.To = 0.95;
            ListBoxOpacity2.Duration = new Duration( TimeSpan.FromSeconds( 0.2 ) );
            ListBoxOpacity2.BeginTime = TimeSpan.FromSeconds( 0.75 );

            ListBoxBorder.BeginAnimation( Border.HeightProperty, ListBoxBorderHeight1 );
            ListBoxBorder.BeginAnimation( Border.HeightProperty, ListBoxBorderHeight2, HandoffBehavior.Compose );
            ListBoxBorder.BeginAnimation( Border.OpacityProperty, ListBoxBorderOpacity1, HandoffBehavior.Compose );
            ListBoxBorder.BeginAnimation( Border.OpacityProperty, ListBoxBorderOpacity2, HandoffBehavior.Compose );
            MListBox.BeginAnimation( System.Windows.Controls.ListBox.OpacityProperty, ListBoxOpacity1, HandoffBehavior.Compose );
            MListBox.BeginAnimation( System.Windows.Controls.ListBox.OpacityProperty, ListBoxOpacity2, HandoffBehavior.Compose );

        }

        void Window_MouseLeftButtonDown( object sender, MouseButtonEventArgs args )
        {
            this.DragMove();

        }

        private void ColorBalance_Selected( object sender, RoutedEventArgs e )
        {
            Dialogs.ColorBalanceDialog cbDlg = new Dialogs.ColorBalanceDialog();
            cbDlg.Show();
        }

        private void ImageSize_Selected( object sender, RoutedEventArgs e )
        {
            Dialogs.ImageSizeDialog imgDlg = new Dialogs.ImageSizeDialog( mWindowTemp );
            imgDlg.Show();
        }

        private void Contrast_Selected( object sender, RoutedEventArgs e )
        {
            Dialogs.ContrastDialog ctDlg = new Dialogs.ContrastDialog( mWindowTemp );
            ctDlg.Show();
        }

        private void CutOut_Selected( object sender, RoutedEventArgs e )
        {
            cutOutItem.IsSelected = false;
            e.Handled = true;

            Dialogs.UsefulCutOut cutDlg = new Dialogs.UsefulCutOut( mWindowTemp );
            //Dialogs.CutOutDialog cutDlg = new Dialogs.CutOutDialog( mWindowTemp, this );

            cutDlg.Show();
        }

        private void Rotate_Selected( object sender, RoutedEventArgs e )
        {
            Dialogs.RotateDialog rtDlg = new Dialogs.RotateDialog( mWindowTemp );
            rtDlg.Show();
        }

        private void ColorPassage_Selected( object sender, RoutedEventArgs e )
        {
            Dialogs.ColorPassageDialog cpDlg = new Dialogs.ColorPassageDialog( mWindowTemp );
            cpDlg.Show();
        }

        bool isListBoxDisappear = false;

        private void newWindow_MouseLeave( object sender, MouseEventArgs e )
        {
            DoubleAnimation ListBoxBorderOpacity = new DoubleAnimation();
            ListBoxBorderOpacity.To = 0.2;
            ListBoxBorderOpacity.Duration = new Duration( TimeSpan.FromSeconds( 0.5 ) );
            ListBoxBorder.BeginAnimation( Border.OpacityProperty, ListBoxBorderOpacity );

            DoubleAnimation ListBoxOpacity = new DoubleAnimation();
            ListBoxOpacity.To = 0.2;
            ListBoxOpacity.Duration = new Duration( TimeSpan.FromSeconds( 0.5 ) );
            MListBox.BeginAnimation( System.Windows.Controls.ListBox.OpacityProperty, ListBoxOpacity );

            isListBoxDisappear = true;
        }

        private void newWindow_MouseEnter( object sender, MouseEventArgs e )
        {
            if ( isListBoxDisappear == true )
            {
                DoubleAnimation ListBoxBorderOpacity = new DoubleAnimation();
                ListBoxBorderOpacity.To = 0.9;
                ListBoxBorderOpacity.Duration = new Duration( TimeSpan.FromSeconds( 0.1 ) );
                ListBoxBorder.BeginAnimation( Border.OpacityProperty, ListBoxBorderOpacity );

                DoubleAnimation ListBoxOpacity = new DoubleAnimation();
                ListBoxOpacity.To = 0.9;
                ListBoxOpacity.Duration = new Duration( TimeSpan.FromSeconds( 0.1 ) );
                MListBox.BeginAnimation( System.Windows.Controls.ListBox.OpacityProperty, ListBoxOpacity );
            }

        }

        private void Close_Selected( object sender, RoutedEventArgs e )
        {
            ImaginationFactory.MainWindow.isListBoxInitial = false;
            this.Close();
        }

        //
        // 保存
        //
        private void saveItem_Selected( object sender, RoutedEventArgs e )
        {
            mWindowTemp.ImageLibrary.MySaveFileDialog();

            saveItem.IsSelected = false;
            e.Handled = true;
        }


        private void OpenItem_Selected( object sender, RoutedEventArgs e )
        {
            Thread thread = new Thread( mWindowTemp.OpenFileThread );
            thread.Start();

            OpenItem.IsSelected = false;
            e.Handled = true;
        }

        private void verticalMirror_Click( object sender, RoutedEventArgs e )
        {
            if ( !mWindowTemp.IsOpen )
                return;

            mWindowTemp.ImageLibrary.MirrorEffect( Mirror_Sign.On_Down );
            mWindowTemp.image.Source = mWindowTemp.ImageLibrary.LoadedImage.Source;

            verticalMirror.IsSelected = false;
            e.Handled = true;
        }

        private void HorizonMirror_Click( object sender, RoutedEventArgs e )
        {
            if ( !mWindowTemp.IsOpen )
                return;

            mWindowTemp.ImageLibrary.MirrorEffect( Mirror_Sign.Left_Right );
            mWindowTemp.image.Source = mWindowTemp.ImageLibrary.LoadedImage.Source;

            HorizonMirror.IsSelected = false;
            e.Handled = true;
        }

        private void SingleColorItem_Selected( object sender, RoutedEventArgs e )
        {
            if ( !mWindowTemp.IsOpen )
                return;

            mWindowTemp.ImageLibrary.SingleColor( BlackAndWhite.Decoloration );
            mWindowTemp.image.Source = mWindowTemp.ImageLibrary.LoadedImage.Source;

            SingleColorItem.IsSelected = false;
            e.Handled = true;
        }

        private void GuassianBlurItem_Selected( object sender, RoutedEventArgs e )
        {
            if ( !mWindowTemp.IsOpen )
                return;

            mWindowTemp.ImageLibrary.GussianBlur( 5, 5 );
            mWindowTemp.image.Source = mWindowTemp.ImageLibrary.LoadedImage.Source;

            GuassianBlurItem.IsSelected = false;
            e.Handled = true;
        }

        private void GT_EqualItem_Selected( object sender, RoutedEventArgs e )
        {
            if ( !mWindowTemp.IsOpen )
                return;

            mWindowTemp.ImageLibrary.GT_Equal();
            mWindowTemp.image.Source = mWindowTemp.ImageLibrary.LoadedImage.Source;

            GT_EqualItem.IsSelected = false;
            e.Handled = true;
        }

        private void ThresholdItem_Selected( object sender, RoutedEventArgs e )
        {
            if ( !mWindowTemp.IsOpen )
                return;

            mWindowTemp.ImageLibrary.Threshold();
            mWindowTemp.image.Source = mWindowTemp.ImageLibrary.LoadedImage.Source;

            ThresholdItem.IsSelected = false;
            e.Handled = true;
        }

        private void ErosionItem_Selected( object sender, RoutedEventArgs e )
        {
            if ( !mWindowTemp.IsOpen )
                return;

            mWindowTemp.ImageLibrary.Erosion();
            mWindowTemp.image.Source = mWindowTemp.ImageLibrary.LoadedImage.Source;

            ErosionItem.IsSelected = false;
            e.Handled = true;
        }

        private void DilationItem_Selected( object sender, RoutedEventArgs e )
        {
            if ( !mWindowTemp.IsOpen )
                return;

            mWindowTemp.ImageLibrary.Dilation();
            mWindowTemp.image.Source = mWindowTemp.ImageLibrary.LoadedImage.Source;

            DilationItem.IsSelected = false;
            e.Handled = true;
        }

        private void InnerEdgeItem_Selected( object sender, RoutedEventArgs e )
        {
            if ( !mWindowTemp.IsOpen )
                return;

            mWindowTemp.ImageLibrary.InnerEdge();
            mWindowTemp.image.Source = mWindowTemp.ImageLibrary.LoadedImage.Source;

            InnerEdgeItem.IsSelected = false;
            e.Handled = true;
        }

        private void OutterEdgeItem_Selected( object sender, RoutedEventArgs e )
        {
            if ( !mWindowTemp.IsOpen )
                return;

            mWindowTemp.ImageLibrary.Dilation();
            mWindowTemp.image.Source = mWindowTemp.ImageLibrary.LoadedImage.Source;

            OutterEdgeItem.IsSelected = false;
            e.Handled = true;
        }

        private void BatchProcessItem_Selected(object sender, RoutedEventArgs e)
        {
            Dialogs.BatchProcessDialog bpDlg = new Dialogs.BatchProcessDialog(mWindowTemp);
            bpDlg.Show();
        }
    }
}
